package com.qyhl.webtv.module_news.news.union.town;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.glide.GlideRoundTransform;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class Town3ListFragment extends BaseFragment implements TownListContract.TownListView {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.recycle_view)
    RecyclerView recyclerView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.barlayout)
    RelativeLayout barlayout;

    private String title;

    private View view;

    private TownListPresenter mPresenter;

    private String sectionId = "";

    private List<UnionBean> mList;

    private RequestOptions options;

    public void setTitle(String title) {
        this.title = title;
    }


    private CommonAdapter commonAdapter;



    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.news_fragment_union_town3, null);
    }

    @Override
    protected void create() {
        mPresenter = new TownListPresenter(this);

        initView();

    }

    @Override
    public void initImmersionBar() {
        if( !StringUtils.isEmpty(title) ){
            ImmersionBar.with(this)
                    .fitsSystemWindows(true)
                    .statusBarDarkFont(true)
                    .statusBarColor(R.color.white)
                    .init();
        }
    }

    @Override
    protected void setListener() {
        setListeners();

    }

    @Override
    protected void onFirstVisible() {
        loadData();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    public static Town3ListFragment newInstance(String title, String url) {
        Town3ListFragment fragment = new Town3ListFragment();
        fragment.setTitle(title);
        fragment.setSectionId(url);
        return fragment;
    }

    private void setSectionId(String url) {
        sectionId = url;
    }



    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        mPresenter.getDatas(sectionId);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
        }
    }



    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        mList = new ArrayList<>();

        if (StringUtils.isEmpty(title)) {
            barlayout.setVisibility(View.GONE);
        } else {
            mTitle.setText(StringUtils.isEmpty(title) ? "乡镇" : title);
        }

        //glide配置
        options = new RequestOptions()
                .placeholder(R.drawable.cover_normal_default)
                .error(R.drawable.cover_normal_default)
                .priority(Priority.HIGH)
                .optionalTransform(new GlideRoundTransform(4));

        //配置刷新
        refresh.setEnableLoadMore(false);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setDisableContentWhenRefresh(true);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));

        recyclerView.setAdapter(commonAdapter =new CommonAdapter<UnionBean>(getContext(), R.layout.news_item_union_town3, mList) {
            @Override
            protected void convert(ViewHolder viewHolder, UnionBean item, int position) {
                TextView title = viewHolder.getView(R.id.title);
                ImageView cover = viewHolder.getView(R.id.cover);

                title.setText(item.getName());
                Glide.with(Town3ListFragment.this)
                        .load(item.getLogo())
                        .apply(options)
                        .into(cover);
            }
        });

    }


    /**
     * 设置监听
     */
    private void setListeners() {
        //重载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mPresenter.getDatas(sectionId);
            }
        });

        //刷新
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getDatas(sectionId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.TOWNDETAIL);
                Bundle bundle = new Bundle();
                bundle.putString("id", mList.get(position).getId());
                bundle.putString("title", mList.get(position).getName());
                bundle.putString("cover", mList.get(position).getLogo());
                RouterManager.router(ARouterPathConstant.DETAIL_TOWN,bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });


    }

    /**
     * 设置数据
     *
     * @param beanList
     */
    @Override
    public void setDatas(List<UnionBean> beanList) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Success);
        mList.clear();
        mList.addAll(beanList);
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        refresh.finishRefresh();
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);
    }

    /**
     * 无网络
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
    }

    @Override
    public void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("底部乡镇");
        MobclickAgent.onResume(getContext());
    }


    @Override
    public void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("底部乡镇");
        MobclickAgent.onPause(getContext());
    }
}
